package chenji.yourticket.editor;

import java.util.HashMap;
import java.util.LinkedHashSet;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import chenji.yourticket.Main;
import chenji.yourticket.inventory.SimpleInventory;
import chenji.yourticket.inventory.SimpleItemStack;

public class MaterialChooseInventory extends SimpleInventory {
	private LinkedHashSet<MaterialInfo> infoList;
	private HashMap<Integer, MaterialInfo> cache = new HashMap<Integer, MaterialInfo>();
	private int maxPage;
	private int nowPage = 0;
	private Player p;

	public MaterialChooseInventory(Player p) {

		super(p, (short) 7, Main.getMessageManager().getMaterialChooseInventory_Title(), 6);
		this.p = p;	this.infoList=Main.getMaterialManager().getTreeSet();
		this.maxPage = this.infoList.size() / 28 + 1;
	
		refuseChengePageItem();
	
		this.chengePage(0);
	}

	@Override
	public void installItemStack() {

	}

	private void refuseChengePageItem() {
		ItemStack upPage = new SimpleItemStack(Material.NETHER_STAR,
				Main.getMessageManager().getItem_UpPage_Lore().replaceAll("%nowPage%",
						Integer.toString(this.maxPage)).replace("%maxpage%", Integer.toString(this.maxPage)),
				Main.getMessageManager().getItem_UpPage_Title());
		ItemStack nextPage = new SimpleItemStack(Material.NETHER_STAR,
				Main.getMessageManager().getItem_NextPage_Lore().replaceAll("%nowPage%",
						Integer.toString(this.maxPage)).replace("%maxpage%", Integer.toString(this.maxPage)),
				Main.getMessageManager().getItem_NextPage_Title());
		this.getInventory().setItem(46, upPage);
		this.getInventory().setItem(52, nextPage);

	}

	private void upPage() {
		this.chengePage(this.nowPage - 1);
	}

	private void nextPage() {
		this.chengePage(this.nowPage + 1);
	}

	private void chengePage(int page) {
		if (this.maxPage <= page || page < 0) {
			return;
		}
		this.clear();
		this.cache.clear();
		int i = 0;
		int start = 28 * page;
		int ca = 0;
	
		for (MaterialInfo info : this.infoList) {
		
			i++;
			if (i >= start && i <= start + 28) {
				ca++;
				cache.put(ca, info);
				this.getInventory().addItem(info.getDisplayItemStack());
			}
		}

		refuseChengePageItem();
	}

	public void ClickInventory(int slot) {

		int l = 0;
		if (slot >= 10 && slot <= 16) {
			l = slot - 9;
		} else if (slot >= 19 && slot <= 25) {
			l = slot - 11;
		} else if (slot >= 27 && slot <= 33) {
			l = slot - 13;
		} else if (slot >= 35 && slot <= 42) {
			l = slot - 15;
		}

		if (slot == 46) {
			this.upPage();
			return;
		}
		if (slot == 52) {
			this.nextPage();
			return;
		}
		if (l == 0) {
			return;
		}
		MaterialInfo mi = this.cache.get(l);
		if (mi == null) {
			return;
		}
		Material m = this.cache.get(l).getMaterial();
		Main.getEditorManager().setNewModelMaterial(p, m);
		p.closeInventory();
		p.sendMessage(Main.getMessageManager().getMessage_Enter_Name());
		return;
	}

	public void clear() {
		int[] i = new int[] { 10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37,
				38, 39, 40, 41, 42, 43 };
		for (int ia : i) {
			this.getInventory().setItem(ia, null);
		}
	}

}
